package ldk.util.adapterwrappers;

import android.support.v7.widget.RecyclerView;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 将RecyclerView的转化为ListView的Adapter
 * Created by ldkxingzhe@163.com on 2017/5/18.
 */
public class RecyclerView2ListViewWrapper extends BaseAdapter{
    @SuppressWarnings("unused")
    private static final String TAG = "RecyclerView2ListViewWrapper";

    private final RecyclerView.Adapter mRealAdapter;
    private SparseIntArray mRecyclerType2ListType = new SparseIntArray();
    private SparseIntArray mListType2RecyclerType = new SparseIntArray();

    public RecyclerView2ListViewWrapper(RecyclerView.Adapter realAdapter) {
        this.mRealAdapter = realAdapter;
        mRealAdapter.registerAdapterDataObserver(mDataObserver);
    }

    @Override
    public int getCount() {
        return mRealAdapter.getItemCount();
    }

    @Override
    public Object getItem(int position) {
        throw new IllegalStateException("getItem不在提供实现");
    }

    @Override
    public int getItemViewType(int position) {
        int type = mRecyclerType2ListType.get(mRealAdapter.getItemViewType(position), -1);
        if (type == -1)
            throw new IllegalStateException("viewType can't be negative");
        return type;
    }

    @Override
    public long getItemId(int position) {
        return mRealAdapter.getItemId(position);
    }

    @Override
    public boolean hasStableIds() {
        return mRealAdapter.hasStableIds();
    }

    @Override
    public int getViewTypeCount() {
        return mListType2RecyclerType.size();
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RecyclerView.ViewHolder viewHolder;
        if (convertView == null){
            int realType = mListType2RecyclerType.get(getItemViewType(position));
            viewHolder = mRealAdapter.createViewHolder(parent, realType);
            mRealAdapter.onBindViewHolder(viewHolder, position);
            convertView = viewHolder.itemView;
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (RecyclerView.ViewHolder) convertView.getTag();
        }
        mRealAdapter.bindViewHolder(viewHolder, position);
        return convertView;
    }

    private void prepareData(){
        mRecyclerType2ListType.clear();
        mListType2RecyclerType.clear();
        int realItemCount = mRealAdapter.getItemCount();
        int currentListViewType = 0;
        for (int i = 0; i < realItemCount; i++){
            int recyclerViewType = mRealAdapter.getItemViewType(i);
            int listViewType = mRecyclerType2ListType.get(recyclerViewType, -1);
            if (listViewType == -1){
                mRecyclerType2ListType.put(recyclerViewType, currentListViewType);
                mListType2RecyclerType.put(currentListViewType, recyclerViewType);
                currentListViewType++;
            }
        }
    }

    private RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            prepareData();
            notifyDataSetChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            onChanged();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            onChanged();
        }
    };
}
